package IT.DataAccess;

import IT.Entities.ClienteContacto;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DatClienteContacto implements IDatClienteContacto{
    
    private List<ClienteContacto> oLista;
    private ClienteContacto oContacto;
    
    private BDConexion oBase;    
    private Connection oConexion;
    private CallableStatement oCst;    
    private ResultSet oRst;    
    
    public List<ClienteContacto> Listar() {
        oLista=new ArrayList<ClienteContacto>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteContacto(oRst.getInt("cod_con_in"), 
                        oRst.getString("nom_con_vc"), 
                        oRst.getString("tel1_con_vc"),
                        oRst.getString("tel2_con_vc"), 
                        oRst.getString("tel3_con_vc"), 
                        oRst.getString("tel4_con_vc"), 
                        oRst.getString("tel5_con_vc"), 
                        oRst.getInt("car_con_in"), 
                        oRst.getString("Cargo"), 
                        oRst.getInt("ar_con_in"), 
                        oRst.getString("Area"), 
                        oRst.getInt("est_con_in"), 
                        oRst.getString("Estado"), 
                        oRst.getInt("cod_cli_in"),
                        oRst.getString("cor_con_vc"),
                        oRst.getBoolean("prin_con_bo")));                
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public ClienteContacto Ind(int nCodigo) {
         oContacto=new ClienteContacto();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                
                oContacto.setCod_con_in(oRst.getInt("cod_con_in"));
                oContacto.setNom_con_vc(oRst.getString("nom_con_vc"));
                oContacto.setTel1_con_vc(oRst.getString("tel1_con_vc"));
                oContacto.setTel2_con_vc(oRst.getString("tel2_con_vc"));
                oContacto.setTel3_con_vc(oRst.getString("tel3_con_vc"));
                oContacto.setTel4_con_vc(oRst.getString("tel4_con_vc"));
                oContacto.setTel5_con_vc(oRst.getString("tel5_con_vc"));
          
                oContacto.setCar_con_in( oRst.getInt("car_con_in"));
                oContacto.setCargo(oRst.getString("Cargo"));
                
                oContacto.setAr_con_in( oRst.getInt("ar_con_in"));
                oContacto.setArea(oRst.getString("Area"));
                        
                oContacto.setEst_con_in(oRst.getInt("est_con_in"));
                oContacto.setEstado(oRst.getString("Estado"));
                oContacto.setCod_cli_in(oRst.getInt("cod_cli_in"));
                oContacto.setCor_con_vc(oRst.getString("cor_con_vc"));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oContacto;
    }

    public List<ClienteContacto> Ubi(int nCliente) {
        oLista=new ArrayList<ClienteContacto>();
         
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoUbi(?)}");
            oCst.setInt(1, nCliente);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new ClienteContacto(oRst.getInt("cod_con_in"), 
                        oRst.getString("nom_con_vc"), 
                        oRst.getString("tel1_con_vc"),
                        oRst.getString("tel2_con_vc"), 
                        oRst.getString("tel3_con_vc"), 
                        oRst.getString("tel4_con_vc"), 
                        oRst.getString("tel5_con_vc"), 
                        oRst.getInt("car_con_in"), 
                        oRst.getString("Cargo"), 
                        oRst.getInt("ar_con_in"), 
                        oRst.getString("Area"), 
                        oRst.getInt("est_con_in"), 
                        oRst.getString("Estado"), 
                        oRst.getInt("cod_cli_in"),
                        oRst.getString("cor_con_vc"),
                        oRst.getBoolean("prin_con_bo")));             
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
         return oLista;
    }

    public void Insertar(ClienteContacto ent) {
        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoInsertar(?,?,?, ?,?,?, ?,?,?, ?,?,?)}");     
            
            oCst.setString(1, ent.getNom_con_vc());
            oCst.setString(2, ent.getTel1_con_vc());
            oCst.setString(3, ent.getTel2_con_vc());
            oCst.setString(4, ent.getTel3_con_vc());
            oCst.setString(5, ent.getTel4_con_vc());
            oCst.setString(6, ent.getTel5_con_vc());
            
            oCst.setInt(7, ent.getCar_con_in());
            oCst.setInt(8, ent.getAr_con_in());
            oCst.setInt(9, ent.getEst_con_in());
            oCst.setInt(10, ent.getCod_cli_in());
            oCst.setString(11, ent.getCor_con_vc());
            oCst.setBoolean(12, ent.isPrin_con_bo());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(ClienteContacto ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoEditar(?,?,?, ?,?,?, ?,?,?, ?,?,?, ?)}");     
            oCst.setInt(1, ent.getCod_con_in());
            oCst.setString(2, ent.getNom_con_vc());
            oCst.setString(3, ent.getTel1_con_vc());
            
            oCst.setString(4, ent.getTel2_con_vc());
            oCst.setString(5, ent.getTel3_con_vc());
            oCst.setString(6, ent.getTel4_con_vc());
            
            oCst.setString(7, ent.getTel5_con_vc());            
            oCst.setInt(8, ent.getCar_con_in());
            oCst.setInt(9, ent.getAr_con_in());
            
            oCst.setInt(10, ent.getEst_con_in());
            oCst.setInt(11, ent.getCod_cli_in());
            oCst.setString(12, ent.getCor_con_vc());
            
            oCst.setBoolean(13, ent.isPrin_con_bo());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Eliminar(int nCodigo) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoEliminar(?)}");                 
            oCst.setInt(1, nCodigo);          
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Valida(ClienteContacto ent) {
        int nResultado=0;
        
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_ClienteContactoValida(?,?,?)}");              
                                   
            oCst.setInt(1, ent.getCod_con_in());
            oCst.setInt(2, ent.getCod_cli_in());            
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            
            oCst.execute();
            nResultado=oCst.getInt(3);
            
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nResultado;
    }

}
